import { ref } from 'vue'
import { defineStore } from 'pinia'
import { userInfoApi } from '@/apis/system/login.js'

const useUserStore = defineStore('user',()=>{
      
    // user信息
    const user = ref({
        id:'',
        name:'',
        avatar:'',
        roles:[],
        permissions:[],
    });
    
    // 获取user信息
    const userInfo=async() => {
        const res = await userInfoApi();
        user.value.id=res.data.user.id;
        user.value.name=res.data.user.nickName;
        user.value.avatar=res.data.user.avatar;
        if(res.data.roles && res.data.roles.length > 0){
            user.value.roles=res.data.roles;
            user.value.permissions = res.data.permissions;
        }else{
            user.value.roles=['ROLE_DEFAULT']
        }
    };

    // 清理token信息
    const clearUser = () => {
        user.value.id = '';
        user.value.name-'';
        user.value.avatar='';
        user.value.roles=[];
        user.value.permissions = [];
    };

    // return
    return {
        user,
        userInfo,
        clearUser,
    }
},)

export default useUserStore